package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.db.domain.MarketIssue;
import com.cskaoyan.market.service.admin.MarketIssueService;
import com.cskaoyan.market.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RequiresAuthentication
@RestController
@RequestMapping("admin/issue")
public class AdminIssueController {

    // 写一个service

    @Autowired
    MarketIssueService issueService;


    @GetMapping("list")
    public Object list(Integer page, Integer limit, String sort, String order, String id, String question, String answer, String addTime, String updateTime) {

        if (StringUtils.isEmpty(sort)) {
            sort = "add_time";
        }
        if (StringUtils.isEmpty(order)) {
            order = "desc";
        }
        // 调用issueService
        List<MarketIssue> marketIssueList = issueService.list(page, limit, sort, order, id, question, answer, addTime, updateTime);
        return ResponseUtil.okList(marketIssueList);
    }

    @PostMapping("create")
    public Object create(@RequestBody MarketIssue marketIssue) {

        // 新增成员列表
        marketIssue = issueService.create(marketIssue);
        return ResponseUtil.ok(marketIssue);
    }

    @PostMapping("delete")
    public Object delete(@RequestBody Map<String, Integer> map) {

        Integer id = map.get("id");
        int row = issueService.delete(id);

        if (row == 1) {
            Object o = ResponseUtil.ok();
            return o;
        }
        return ResponseUtil.fail();
    }

    @PostMapping("update")
    public Object update(@RequestBody Map<String, String> map) {

        Integer id = Integer.valueOf(map.get("id"));
        String question = map.get("question");
        String answer = map.get("answer");
        String addTime = map.get("addTime");
        String updateTime = map.get("updateTime");
        int row = issueService.update(id, question, answer, addTime, updateTime);
        if (row == 1) {
            return ResponseUtil.ok();
        }
        return ResponseUtil.fail();
    }
}